﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using NetDon.OA.Interface;
using NetDon.OA.Interface.eLogistics;
using NetDon.Utilities;

namespace NetDon.OA.Portal.Finance
{
    [Navigation("干线流程", "付款台账编辑")]
    public partial class PaymentMachineAccountInfoEdit : NavigationPageBase
    {
        public Guid Id
        {
            get { return Parameters.GetGuid("Id"); }
        }

        public Guid IssuingOrderId
        {
            get { return Parameters.GetGuid("IssuingOrderId"); }
        }

        public bool IsViewModel
        {
            get { return Parameters.GetBoolean("isViewModel", true); }
        }


        


        public MachineAccountType PaymentOrIncome
        {
            get { return Parameters.GetEnum<MachineAccountType>("paymentOrIncome", MachineAccountType.Payment); }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                CancelButton = btnCancel;
                LoadInitialData();
            }
        }

        private void LoadInitialData()
        {
            var ordrerService = ServiceContainer.GetService<IIssuingOrderService>();
            var order = ordrerService.GetSimpleIssuingOrder(IssuingOrderId);


            this.txtOrderCode.Text = order.OrderCode;
            this.lblRouteName.Text = GetDictionaryItemName(order.RouteTypeId);
            this.lblTransporationPlanCode.Text = order.TransportationPlanCode;

            this.txtOrderCode1.Text = order.OrderCode;
            if (order.GoodsUnloadingCost.HasValue && order.GoodsUnloadingCost.Value > 0)
            {
              
                this.txtGoodsUnloadingCost.Text = order.GoodsUnloadingCost.Value.ToString("#,###.00");
            }


            if (Id == Guid.Empty)
            {
                this.radioMachineAccountPayment.Checked = true;
                this.txtPaymentOrIncomeDate.Text =
                    this.txtPaymentOrIncomeDate1.Text = DateTime.Now.ToString("yyyy-MM-dd");
                InitialPaymentOrIncomeUI(PaymentOrIncome == MachineAccountType.Payment);
                if (PaymentOrIncome == MachineAccountType.Payment)
                {
                    var service = ServiceContainer.GetService<IMachineAccountService>();
                    var data = service.GetMachineAccountPaymentSummary(new MachineAccountData() { IssuingOrderId = IssuingOrderId });
                    this.txtDueShippingCost.Text = data.DueShippingCost.ToString("#,###.00");
                    this.txtPendingShippingCost.Text = data.PendingShippingCost.ToString("#,###.00");
                    this.txtPaidShippingCost.Text = data.PaidShippingCost.ToString("#,###.00");
                    this.txtToBePaidShippingCost.Text = data.PendingShippingCost.ToString("#,###.00");

                }
            }
            else
            {
                InitialPaymentOrIncomeUI(PaymentOrIncome == MachineAccountType.Payment);
                var service = ServiceContainer.GetService<IMachineAccountService>();
                var data = service.GetMachineAccounts(Id, PaymentOrIncome);
                FormPanelIncome.DataSource = data;

                //var revenueMachinAccount = ServiceContainer.GetService<IRevenueMachineAccountService>().GetRevenueMachineAccount(IssuingOrderId);


                if (PaymentOrIncome == MachineAccountType.Income)
                {
                    this.txtOrderCode.Text = order.OrderCode;
                    this.lblRouteName.Text = GetDictionaryItemName(order.RouteTypeId);
                    this.lblTransporationPlanCode.Text = order.TransportationPlanCode;
                    this.radioMachineAccountIncome.Checked = true;
                    this.radioMachineAccountIncome.Enabled = false;
                    this.radioMachineAccountPayment.Visible = false;

                    FormPanelIncome.DataBind();
                    if (data.IncomeType.HasValue)
                        this.ddlIncomeType.SelectedValue = data.IncomeType.Value.ToString();

                    FormPanelIncome.Enabled = false;


                }
                else
                {
                    this.txtOrderCode1.Text = order.OrderCode;
                    this.radioMachineAccountPayment.Checked = true;
                    this.radioMachineAccountPayment.Enabled = false;
                    radioMachineAccountIncome.Visible = false;

                    FormPanelPayment.DataSource = data;
                    FormPanelPayment.DataBind();

                    this.ddlIncomeType.SelectedValue = data.IncomeType.ToString();
                    this.ddlPaymentMethod.SelectedValue = data.PaymentMethod.ToString();
                    txtDueShippingCost.Enabled = false;
                    txtPaidShippingCost.Enabled = false;
                    txtPendingShippingCost.Enabled = false;
                    txtPaymentOrIncomeDate.Enabled = false;
                    txtToBePaidShippingCost.Enabled = false;
                    txtOtherAmountFromManufactor.Enabled = false;
                    ddlPaymentMethod.Enabled = false;
                    txtDeductionExceptionsAmount.Enabled = false;
                    txtDeductionExceptionsComments.Enabled = false;

                    this.txtGoodsUnloadingCost.Enabled = true;
                }
            }
        }



        protected void btnSave_Click(object sender, EventArgs e)
        {
            var service = ServiceContainer.GetService<IMachineAccountService>();
            MachineAccountData machineAccountData = null;
            if (Id == Guid.Empty)
            {
                machineAccountData = new MachineAccountData();
            }
            else
            {
                machineAccountData = service.GetMachineAccounts(Id, PaymentOrIncome);
            }
            var order = ExtractIssuingOrder( machineAccountData);

            //check
            if (PaymentOrIncome == MachineAccountType.Payment)
            {
                if (Id == Guid.Empty) //only check when add new 
                {
                    //支付金额+已付运费+扣罚金额≤应付运费
                    if (machineAccountData.DueShippingCost <
                        (machineAccountData.PaidShippingCost + machineAccountData.TobePaidShippingCost -
                         machineAccountData.DeductionExceptionsAmount))
                    {
                        Alert("本次支付金额输入过大。（条件:本次支付金额+已付运费+扣罚司机金额≤应付运费）");
                        return;
                    }
                }
            }
            bool result = false;

            result = Id != Guid.Empty ? service.ModifyMachineAccounts(order) : service.AddMachineAccounts(order);
            if (result)
            {
                Success("保存成功！");
                this.CloseWindow(true);
            }

        }

        private MachineAccountData ExtractIssuingOrder( MachineAccountData machineAccountData)
        {

            if (radioMachineAccountIncome.Checked)
            {
                machineAccountData.PaymentOrIncome = MachineAccountType.Income;
                machineAccountData.IncomeAmount = this.txtIncomeAmount.Text.ToDecimal() ?? 0;
                machineAccountData.OtherIncome = this.txtOtherIncome.Text.ToDecimal() ?? 0;
                machineAccountData.IncomeType = this.ddlIncomeType.SelectedValue.ToGuid();
                machineAccountData.PaymentOrIncomeDate = DateTime.Parse(this.txtPaymentOrIncomeDate1.Text);
                machineAccountData.LastUpdatedBy = CurrentApplicationContext.UserId;
                machineAccountData.LastUpdatedDate = DateTime.Now;
                machineAccountData.AreaId = CurrentApplicationContext.CompanyId;
                machineAccountData.OrderCode = this.txtOrderCode.Text.Trim();
                machineAccountData.IssuingOrderId = IssuingOrderId;
            }
            else
            {
                machineAccountData.PaymentOrIncome = MachineAccountType.Payment;
                machineAccountData.OrderCode = this.txtOrderCode.Text.Trim();
                machineAccountData.TransportationPlanCode = this.lblTransporationPlanCode.Text;
                machineAccountData.DueShippingCost = this.txtDueShippingCost.Text.ToDecimal() ?? 0;
                machineAccountData.PaidShippingCost = this.txtPaidShippingCost.Text.ToDecimal() ?? 0;
                machineAccountData.PendingShippingCost = this.txtPendingShippingCost.Text.ToDecimal() ?? 0;
                machineAccountData.TobePaidShippingCost = this.txtToBePaidShippingCost.Text.ToDecimal() ?? 0;
                machineAccountData.GoodsUnloadingCost = this.txtGoodsUnloadingCost.Text.ToDecimal() ?? 0;
                machineAccountData.OtherAmountFromManufactor = this.txtOtherAmountFromManufactor.Text.ToDecimal() ?? 0;
                machineAccountData.IncomeAmount = this.txtIncomeAmount.Text.ToDecimal() ?? 0;
                machineAccountData.OtherIncome = this.txtOtherIncome.Text.ToDecimal() ?? 0;
                machineAccountData.PaymentMethod = this.ddlPaymentMethod.SelectedValue.ToGuid();
                machineAccountData.DeductionExceptionsAmount = this.txtDeductionExceptionsAmount.Text.ToDecimal() ?? 0;
                machineAccountData.DeductionExceptionsComments = this.txtDeductionExceptionsComments.Text.Trim();
                machineAccountData.PaymentOrIncomeDate = DateTime.Parse(this.txtPaymentOrIncomeDate.Text);
                machineAccountData.LastUpdatedBy = CurrentApplicationContext.UserId;
                machineAccountData.LastUpdatedDate = DateTime.Now;
                machineAccountData.AreaId = CurrentApplicationContext.CompanyId;
                machineAccountData.IssuingOrderId = IssuingOrderId;
                machineAccountData.IsFlag = false;

            }

            if (Id == Guid.Empty)
            {
                machineAccountData.CreateBy = CurrentApplicationContext.UserId;
                machineAccountData.CreatedDate = DateTime.Now;
                machineAccountData.Id = Guid.NewGuid();
            }
            else
            {
                machineAccountData.Id = Id;
            }
            return machineAccountData;
        }


        public void radioMachineAccount_CheckedChanged(object sender, EventArgs e)
        {
            var radioButton = (sender as RadioButton);
            InitialPaymentOrIncomeUI(radioButton == this.radioMachineAccountPayment && radioButton.Checked);

        }
        private void InitialPaymentOrIncomeUI(bool isPayment)
        {
            this.FormPanelIncome.Visible = !isPayment;
            this.FormPanelPayment.Visible = isPayment;
        }


    }
}